<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var imgArr = ["../img/1.jpg" ,"../img/2.jpg" ,"../img/3.jpg" ,"../img/4.jpg" ,"../img/5.jpg"]
        var index = 0;


        window.onload = function(){
        

        //给上一张按钮绑定单击事件
        document.getElementById('bt1').onclick = function(){
            //获取img节点
            var img1 = document.getElementsByTagName('img')[0];
            index --;
            if(index < 0){
                index = imgArr.length-1;
            }
            img1.src = imgArr[index];
            //获取p标签节点
            var p = document.getElementById('info');
            p.innerHTML = '总共'+imgArr.length+'张图片,当前第'+(index+1)+'张'
        }



        //给下一张按钮绑定单击事件
        document.getElementById('bt2').onclick = function(){
            //获取img节点
            var img2 = document.getElementsByTagName('img')[0];
            index ++;
            if(index > imgArr.length-1){
                index = 0;
            }
            img2.src = imgArr[index];
            //获取p标签节点
            var p = document.getElementById('info');
            p.innerHTML = '总共'+imgArr.length+'张图片,当前第'+(index+1)+'张'
        }
        }
        

    </script>
    <style>
        *{
            margin: 0%;
            padding: 0%;
        }
        #d1{

            padding: 10px;
            margin: 50px auto;
            width: 500px;
            background-color: wheat;
            text-align: center;
        }
        img{
            width: 500px;
            height: 300px;
        }
    </style>
</head>
<body>
    <div id="d1">
        <p id="info">总共5张图片,当前第1张</p>
        <img src="../img/1.jpg" alt="原神图片">
        <button id="bt1">上一张</button>
        <button id="bt2">下一张</button>
    </div>
</body>
</html>